package net.oschina.app;

import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
import android.widget.EditText;

import com.robotium.solo.Solo;

import junit.framework.AssertionFailedError;

/**
 * Created by Aaron on 2017/3/5.
 */

public class BasicTestCase extends ActivityInstrumentationTestCase2<AppStart> {
    private Solo solo;
    private SoloWrap soloWrap;
    boolean isNeedLogin = false;
    public Helper helper;

    public BasicTestCase() {
        super(AppStart.class);
    }

    public BasicTestCase(boolean isNeedLogin) {
        super(AppStart.class);
        this.isNeedLogin = isNeedLogin;
    }

    @Override
    public void setUp() throws Exception {
        try{
            super.setUp();
            solo = new Solo(getInstrumentation(), getActivity());
            soloWrap = new SoloWrap(solo);
            helper = new Helper(soloWrap);
            Log.i("AUTO", "=======================helloworld1");
//            int b = 3 / 0;
            solo.waitForText("推荐博客", 1, 10000, false);
            solo.clickOnView(solo.getView("net.oschina.app:id/nav_item_me"));
            if (isNeedLogin) {
                if (!solo.waitForText("autotest", 1, 2000, false, true)) {
                    solo.clickOnView(solo.getView("net.oschina.app:id/iv_portrait"));
                    solo.waitForText("注册", 1, 1000);
                    EditText name = (EditText) solo.getView("net.oschina.app:id/et_username");
                    solo.clearEditText(name);
                    solo.enterText(name, "735723619@qq.com");
                    EditText pwd = (EditText) solo.getView("net.oschina.app:id/et_password");
                    solo.clearEditText(pwd);
                    solo.enterText(pwd, "12345678");
                    solo.clickOnView(solo.getView("net.oschina.app:id/btn_login"));
                }
            } else {
                if (solo.waitForText("autotest", 1, 2000, false, true)) {
                    solo.clickOnView(solo.getView("net.oschina.app:id/iv_logo_setting"));
                    solo.clickOnText("注销");
                }
            }
            solo.clickOnView(solo.getView("net.oschina.app:id/nav_item_news"));
        }catch (Exception ex){
            Log.i("AUTO","Exception happened in setup section, error msg is "+ex.getMessage().toString());
            solo.takeScreenshot(this.getClass().getSimpleName());
            throw ex;
        }

    }

    @Override
    protected void runTest() throws Throwable {
        try{
            super.runTest();
        }catch (Throwable th){
            if(th instanceof Exception){
                Log.i("AUTO","AssertionFailedError is child of Exception");
            }
            if(th != null){
                Log.i("AUTO","Exception happened in runtest section, error msg is "+ th.getMessage());
            }
            solo.takeScreenshot(this.getClass().getSimpleName());
            throw  th;
        }

    }

    @Override
    public void tearDown() throws Exception {
        try{

        } catch (Throwable th){
            Log.i("AUTO","Exception happened in teardown section, error msg is "+th.getMessage());
            solo.takeScreenshot(this.getClass().getSimpleName());
            throw th;
        }finally {
            solo.finishOpenedActivities();
            super.tearDown();
        }
    }
}
